<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/3/27
 * Time: 11:46
 */

namespace app\admin\controller;

use app\admin\model\AdminUser as AdminUserModel;
use app\admin\validate\AdminValidate;
use app\service\traits\Crud;
use app\admin\model\AdminRole;

class AdminUser extends Base
{
    private AdminUserModel $model;

    use Crud;

    public function initialize()
    {
        parent::initialize();
        $this->model            = new AdminUserModel();
        $this->validateClass    = AdminValidate::class;
        $this->withSearch       = "status,id,real_name,user_name,mobile,identity";
        $this->checkUniqueField = ['user_name'];
        $this->append           = 'identity_text';
    }

    public function format($list): mixed
    {
        return $list->each(function ($item) {
            $roleIds          = $item->adminUserRoles()->column('admin_role_id');
            $arr              = AdminRole::whereIn('id', $roleIds)->field('id,name')->select();
            $item->role_names = implode('/', array_column($arr->toArray(), 'name'));
            $item->role_ids   = implode(',', array_column($arr->toArray(), 'id'));
        });
    }
}